package com.aopcinemas.visitor.behaviour;

import jade.content.lang.Codec.CodecException;
import jade.content.onto.OntologyException;
import jade.content.onto.basic.Action;
import jade.lang.acl.ACLMessage;
import jade.proto.AchieveREInitiator;

import com.aopcinemas.cinema.model.Schedule;
import com.aopcinemas.cinema.ontology.GetScheduleList;
import com.aopcinemas.visitor.VisitorAgent;

public class ScheduleInfoInitiator extends AchieveREInitiator {

    private static final long serialVersionUID = 1L;

    private ScheduleInfoNotifyBehavior notifier;

    public ScheduleInfoInitiator(VisitorAgent agent, ScheduleInfoNotifyBehavior notifier, ACLMessage message) {
        super(agent, message);
        this.notifier = notifier;
    }

    @Override
    protected void handleInform(ACLMessage inform) {
        System.out.println("SessionListBehaviour" + inform.getOntology() + ", " + inform.getLanguage());
        try {
            Object content = myAgent.getContentManager().extractContent(inform);
            if (content instanceof Action) {
                GetScheduleList getScheduleList = (GetScheduleList) ((Action) content).getAction();
                if (getScheduleList.getSchedules() != null) {
                    for (Schedule schedule : getScheduleList.getSchedules()) {
                        schedule.setCinema(inform.getSender());
                    }
                }
                notifier.addSchedules(getScheduleList.getSchedules());
            } else {
                System.out.println("Badjaga!");
            }
        } catch (CodecException e) {
            e.printStackTrace();
        } catch (OntologyException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void handleFailure(ACLMessage failure) {
        System.out.println("Uups, failure!");
    }

    @Override
    protected void handleInconsistentFSM(String current, int event) {
        System.out.println("Uups, inconsistent!");
    }

}
